package com.pig4cloud.pig.demo.Util;

import com.pig4cloud.pig.demo.Service.ArticleService;
import com.pig4cloud.pig.demo.Service.TitleService;
import com.pig4cloud.pig.demo.feign.Entity.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.List;

@Component
public class ArticleContentPipeline implements Pipeline {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private TitleService titleService;

    @Override
    public void process(ResultItems resultItems, Task task) {
        List<Article> lists = resultItems.get("content");

        //从种类表中获取对应的id
        String category = resultItems.get("category");
        Long id = titleService.findByCategory(category);

        if (id != null) {
            for (Article list : lists) {
                list.setTid(id);
                articleService.insertArticle(list);
            }
        }

    }
}
